package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author ck
 * @date 2021/11/18 14:30
 */


@DubboService
public class VideoApiImpl implements VideoApi{


    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findVideosByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).
                skip((page -1) * pagesize).
                with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public Video find(ObjectId id) {
        Query query = Query.query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query,Video.class);
    }

    @Override
    public List<Video> findByUid(Integer page,Integer pagesize,Long uid) {
        Query query = Query.query(Criteria.where("userId").is(uid)).skip((page -1) * pagesize).limit(pagesize);
        List<Video> videos = mongoTemplate.find(query, Video.class);
        return videos;
    }
}
